//
//  CatalogDAL.m
//  readNovels
//
//  Created by 吳瀾洲 on 2020/4/27.
//  Copyright © 2020 wulanzhou. All rights reserved.
//

#import "CatalogDAL.h"
#import "BookDBManager.h"
#import "CatalogSQL.h"
#import "BookSQL.h"
#import "Book.h"

@implementation CatalogDAL


//新增目录
- (BOOL)addCatalogs:(NSArray <Catalog *> *)catalogs bookId:(NSInteger)bId{
    if (catalogs && [catalogs count] > 0) {
        
        NSDateFormatter*dateFormat =[[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSMutableArray *t_sql = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *args = [NSMutableArray arrayWithCapacity:0];
        NSInteger index = 0;
        for (Catalog *item in catalogs) {
            item.book_id = bId;
            item.sort = index + 1;
            item.serial_number = [NSString stringWithFormat:@"%ld",index + 1];
            [t_sql addObject:insert_catalog_sql];
            
            //book_id,serial_number,name,page_url,sort,memo,ctime,mtime
            NSMutableArray *values = [NSMutableArray arrayWithCapacity:0];
            [values addObject:[NSNumber numberWithInteger:bId]];
            [values addObject:item.serial_number];
            [values addObject:item.name];
            [values addObject:item.page_url];
            [values addObject:[NSNumber numberWithInteger:item.sort]];
            if (item.memo && [item.memo length]>0) {
                [values addObject:item.memo];
            }else{
                [values addObject:[NSNull null]];
            }
            
            
            
            if (item.ctime) {
                [values addObject:[dateFormat stringFromDate:item.ctime]];
            }else{
                [values addObject:[dateFormat stringFromDate:[NSDate date]]];
            }
            if (item.mtime) {
                [values addObject:[dateFormat stringFromDate:item.mtime]];
            }else{
                [values addObject:[dateFormat stringFromDate:[NSDate date]]];
            }
            [args addObject:values];
            
            index ++;
        }
        
        //更新书籍  dal层不写业务
        /**
        [t_sql addObject:update_book_status];
        
        NSMutableArray *values = [NSMutableArray arrayWithCapacity:0];
        [values addObject:[NSNumber numberWithInteger:BookStatusCatalog]];
        [values addObject:[dateFormat stringFromDate:[NSDate date]]];
        [values addObject:[NSNumber numberWithInteger:bId]];
        [args addObject:values];
         ***/
        
        
        BOOL success = [[BookDBManager shareInstance] transactionUpdataDBWithSqlArray:t_sql arguments:args];
        if (success) {
            NSLog(@"新增书籍目录成功!");
        }else{
            NSLog(@"新增书籍目录失败!");
        }
        return success;
        
    }
    return NO;
}

#pragma mark - DBInterface Methods
- (BOOL)Insert:(Catalog *)entity{
    
    NSDateFormatter*dateFormat =[[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    BOOL success = [[BookDBManager shareInstance] updataDBWithSql:insert_catalog_sql,[NSNumber numberWithInteger:entity.book_id],entity.serial_number,entity.name,entity.page_url,[NSNumber numberWithInteger:entity.sort],entity.memo,[dateFormat stringFromDate:entity.ctime],[dateFormat stringFromDate:entity.mtime], nil];
    if (success) {
        NSLog(@"新增目录成功!");
    }else{
        NSLog(@"新增目录失败!");
    }
    return success;
    
}

- (Catalog *)Entity:(id)kId{
    
    NSInteger primaryKey = [kId integerValue];
    NSArray *source = [[BookDBManager shareInstance] queryDBWithModels:[Catalog class] sql:query_id_catalog,[NSNumber numberWithInteger:primaryKey], nil];
    
    if (source && [source count] >0) {
        
        return source[0];
    }
    return nil;
}

- (BOOL)Delete:(id)kId{

    NSInteger primaryKey = [kId integerValue];
    return [[BookDBManager shareInstance] updataDBWithSql:delete_catalog_book_id,[NSNumber numberWithInteger:primaryKey], nil];
    //delete_catalog_book_id
}

- (NSArray*)list:(id)map{
    
    return [[BookDBManager shareInstance] queryDBWithModels:[Catalog class] sql:query_all_catalog, nil];
}

@end
